import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.swing.plaf.TableHeaderUI;
import java.util.List;

public class JedisList {

    private static void test1(Jedis jedis) {
        System.out.println("---------------------------");
        System.out.println("lpush 和 lrange");
        jedis.flushAll();

        jedis.lpush("key1", "value1", "value2", "value3");
        List<String> result = jedis.lrange("key1", 0 ,-1);

        System.out.println(result);
    }

    private static void test2(Jedis jedis) {
        System.out.println("---------------------------");
        System.out.println("rpush");
        jedis.flushAll();

        jedis.rpush("key1", "value1", "value2", "value3");
        List<String> result = jedis.lrange("key1", 0 ,-1);

        System.out.println(result);
    }

//    private static void test3(Jedis jedis) throws InterruptedException {
//        System.out.println("---------------------------");
//        System.out.println("blpop");
//        jedis.flushAll();
//
//        Thread thread1 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                //体现阻塞
//                try {
//                    Thread.sleep(3000);
//                    jedis.lpush("key1", "v1", "v2");
//                    System.out.println("key1: " + jedis.lrange("key1", 0 , -1));
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        });
//        thread1.start();
//
//        Thread thread2 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                //返回结果是一个 “二元组” ，一是从哪个 key 删除的，二是删除的什么元素
//                List<String> result = jedis.blpop(10, "key1");
//                System.out.println(result.get(0));
//                System.out.println(result.get(1));
//            }
//        });
//        thread2.start();
//    }

    private static void test3(Jedis jedis) throws InterruptedException {
        System.out.println("---------------------------");
        System.out.println("blpop");
        jedis.flushAll();

        List<String> result = jedis.blpop(100, "key1");
        System.out.println(result.get(0));
        System.out.println(result.get(1));
    }



    public static void main(String[] args) throws InterruptedException {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test1(jedis);
            test2(jedis);
            test3(jedis);
        }
    }

}
